//
//  MBProgressHUD+Add.m
//  视频客户端
//
//  Created by mj on 13-4-18.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MBProgressHUD+Add.h"
#import "ALMacro.h"
#import "ADScreenAdaptation.h"
#import "PureLayout.h"
#import "ALMacro.h"
#import "UIFont+category.h"

const NSTimeInterval hubDelayTime = 1.5;

@implementation MBProgressHUD (Add)

#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:hubDelayTime];
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = YES;
    return hud;
}



+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}

+ (void)hideHUDForView:(UIView *)view
{
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}





+ (void)showUserNameORPassWordNotNull:(NSString *)message toView:(UIView *)view{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = message;
    hud.mode = MBProgressHUDModeText;
    hud.yOffset = 100;
    hud.removeFromSuperViewOnHide = YES;
    hud.opacity = 0.8;
    hud.labelFont = [UIFont systemFontOfSize:14.f];
    [hud hide:YES afterDelay:0.8];
}

+ (void)showNoNetWork:(NSString *)text toView:(UIView *)view{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = text;
    hud.mode = MBProgressHUDModeText;
    hud.yOffset = -60;
    hud.removeFromSuperViewOnHide = YES;
    hud.opacity = 0.8;
    hud.labelFont = [UIFont systemFontOfSize:14.f];
    [hud hide:YES afterDelay:1.5];
}

//显示文本，过一段时间消失
+ (void)showText:(NSString *) message toView:(UIView *) view{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = message;
    hud.mode = MBProgressHUDModeText;
    hud.yOffset = 0;
    hud.removeFromSuperViewOnHide = YES;
    hud.opacity = 0.8;
    hud.labelFont = [UIFont systemFontOfSize:15.f];
    [hud hide:YES afterDelay:1.0];
}





//显示一个下载圈
+ (MBProgressHUD *)showDownLoadSqureToView: (UIView*) view{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = NO;
    return hud;
}

+ (MBProgressHUD *)showIndeterminateToView: (UIView*) view {
    
//    return [MBProgressHUD showCustomLoadingToView:view];

    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.color = kRGBFromHexadecimal(0x131522);
    hud.alpha = 0.55;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = NO;
    return hud;
}

+ (MBProgressHUD *)showCustomLoadingToView:(UIView *)view{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.color = [UIColor clearColor];
    hud.removeFromSuperViewOnHide = YES;
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, adsaW(104), adsaW(36))];
    backView.alpha = 0.55;
    backView.backgroundColor = kRGBFromHexadecimal(0x131522);
    backView.layer.cornerRadius = adsaW(3);
    backView.layer.masksToBounds = YES;
    
    UIImage *image = [[UIImage imageNamed:@"tosat 加载中"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anima.toValue = @(M_PI*2);
    anima.duration = 1.0f;
    anima.repeatCount = 10;
    [backView addSubview:imgView];
    [imgView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [imgView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:adsaW(20)];
    [imgView sizeToFit];
    [imgView.layer addAnimation:anima forKey:nil];
    
    UILabel *label = [[UILabel alloc] init];
    label.attributedText = [[NSAttributedString alloc] initWithString:@"加载中"
                                                           attributes:@{NSFontAttributeName :[UIFont scale_fontWithName:kFontName_PingFangSC_Regular size:14] ,
                                                                        NSForegroundColorAttributeName: kRGBFromHexadecimal(0xFFFFFF)}];
    [label sizeToFit];
    [backView addSubview:label];
    [label autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [label autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:adsaW(21)];
    
    hud.customView = backView;
    return hud;
}



@end
